/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package is202.hrms.web;

import is202.hrms.ejb.ModulenEJB;
import is202.hrms.ejb.ProgressEJB;
import is202.hrms.entity.Modulen;
import is202.hrms.entity.Progress;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

/**
 *
 * @author group tre musketter
 */
@Named("progresslistbean")
@RequestScoped
public class ProgressListBean implements Serializable {

    private static final long serialVersionUID = 1L;

    @EJB
    ProgressEJB ProgressEjb;
    @EJB
    ModulenEJB modEjb;

    public ProgressListBean() {
    }

    public List<Progress> getProgress() {
        List<Progress> l = ProgressEjb.findAll();

        return l;
    }

    public ArrayList<Modulen> getModulStudents(int id) {
        ArrayList<Modulen> temp = new ArrayList<Modulen>();
        for (Progress p : getProgress()) {
            if (p.getBruker().getId() == id) {
                temp.add(p.getModuler());
            }

        }
        return temp;
    }

    public List<ModulWrapper> getAvailableModules(int id) {

        Modulen modul = null;
        int modIdNext = 0;
        boolean others = false;
        List<ModulWrapper> modulListe = new ArrayList<ModulWrapper>();
        for (Progress p : getProgress()) {

            p.getModuler().getDato();
            if (p.getBruker().getId() == id) {
                if (p.isApproved()) {
                    modIdNext = p.getModuler().getModulenId();
                } else {
                    others = true;
                    System.out.println();
                }
                if (p.isApproved()) {
                    modulListe.add(new ModulWrapper(modEjb.find(p.getModuler().getModulenId()), "Ferdig"));
                } else {
                    modulListe.add(new ModulWrapper(modEjb.find(p.getModuler().getModulenId()), "Påbegynt modul"));
                }

            }

        }
        if (!others) {
            modIdNext += 1;
            modul = modEjb.find(modIdNext);
            modulListe.add(new ModulWrapper(modul, "Ikke startet"));
        }

        return modulListe;

    }

    public ArrayList<Progress> getProgressStudents(int id) {
        ArrayList<Progress> temp = new ArrayList<Progress>();
        for (Progress p : getProgress()) {
            if (p.getBruker().getId() == id) {
                temp.add(p);
            }

        }
        return temp;
    }

    public Modulen getModulStudent(int id) {
        for (Progress p : getProgress()) {
            if (p.getBruker().getId() == id) {
                return p.getModuler();
            }

        }
        return null;
    }

}
